package com.sky.aspect;
import com.sky.anno.AutoFill;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Aspect
@Slf4j
@Component
public class AutoFillAspect {
    //公共字段自动填充通知
    @Before("@annotation(com.sky.anno.AutoFill)")
    public void autoFill(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        //目标：需要补充目标方法中对象是基础属性字段值
        log.info("公共字段填充。。。。。");

        //1获取目标方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        //2获取目标方法的参数
        Object[] args = joinPoint.getArgs();
        Object entity = null;
        if (args == null || args.length == 0) {
            //如果没有参数，直接结束方法
            return;
        }
        //如果有参数,获取第一个参数即可--要补充属性值的目标对象
        entity = args[0];

        try {
            //3判断新增还是更新--获取目标方法上的注解--通过注解里面的属性值判断
            AutoFill annotation = method.getAnnotation(AutoFill.class);
            OperationType operationType = annotation.value();
            if (operationType.equals(OperationType.INSERT)) {
                //代表是新增方法--补充4个字段:创建时间,创建人,更新时间,更新人
                log.info("公共字段填充[INSERT］．．．．");
                Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
                Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);
                setCreateTime.invoke(entity, LocalDateTime.now());
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setCreateUser.invoke(entity, BaseContext.getCurrentId());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            } else {
                //代表是更新--补充两个字段：更新时间，更新人
                log.info("公共字段自动填充【UPDATE】。。。。。");
                Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class);
                setUpdateTime.invoke(entity, LocalDateTime.now());
                setUpdateUser.invoke(entity, BaseContext.getCurrentId());
            }
        } catch (Exception e) {
            log.error("公共字段填充失败：{}", e.getMessage());
            throw new BaseException("公共自动填充失败");
        }
    }
}
